package com.frontop.system.modules.attendance.workover.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.frontop.common.util.JwtUtil;
import com.frontop.common.util.ResultJsonUtil;
import com.frontop.system.modules.attendance.workover.model.AttendanceWorkOver;
import com.frontop.system.modules.attendance.workover.service.AttendanceWorkOverService;
import com.frontop.system.modules.attendance.workover.vo.AttendanceWorkOverVO;
import com.frontop.system.modules.sys.model.SysOrganization;
import com.frontop.system.modules.project.model.SysProject;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 考勤-加班记录表 前端控制器
 * </p>
 *
 * @author YangJia
 * @since 2022-01-11
 */
@RestController
@RequestMapping("/attendance-work-over")
public class AttendanceWorkOverController {
    @Autowired
    AttendanceWorkOverService attendanceWorkOverService;

    @ApiOperation(value = "添加下班记录")
    @PostMapping("/addAttendanceWorkOver")
    public ResultJsonUtil<Object> addAttendanceWorkOver(@RequestBody(required = false)List<JSONObject> dates,
                                                        Authentication authentication,
                                                        HttpServletRequest request) {
        Claims claims = JwtUtil.getJwtInfo(authentication);
        String userName = (String) claims.get("user_name");
        attendanceWorkOverService.addAttendanceWorkOver(dates,userName);
       return new ResultJsonUtil().success("请求成功");
    }


    @ApiOperation(value = "查询所有上报的加班记录")
    @PostMapping("/getAllWorkOver")
    public ResultJsonUtil<Object> getAllWorkOver(@RequestBody(required = false)List<JSONObject> objects,
                                                        Authentication authentication,
                                                        HttpServletRequest request) {
        Claims claims = JwtUtil.getJwtInfo(authentication);
        String userName = (String) claims.get("user_name");
        return new ResultJsonUtil().success(attendanceWorkOverService.getAllWorkOverByUserName(userName));
    }

    @ApiOperation(value = "导入企业微信报表")
    @PostMapping("/uploadQw")
    public ResultJsonUtil<Object> uploadQw(@RequestParam("file") MultipartFile multipartFile,
                                           @RequestParam("id") String id,
                                                 Authentication authentication,
                                                 HttpServletRequest request) {
        Claims claims = JwtUtil.getJwtInfo(authentication);
        String userName = (String) claims.get("user_name");
        attendanceWorkOverService.addAttendanceWorkOverByExcel(multipartFile,userName);
        return new ResultJsonUtil().success("");
    }


    @ApiOperation(value = "获取可调休列表")
    @PostMapping("/getCompensatoryList")
    public ResultJsonUtil<Object> getCompensatoryList(@RequestBody(required = false)AttendanceWorkOverVO vo,
                                                      Authentication authentication,
                                                      HttpServletRequest request) {
        Claims claims = JwtUtil.getJwtInfo(authentication);
        String userName = (String) claims.get("user_name");
        vo.setUserName(userName);
        if(vo.getWorkDate() != null && vo.getWorkDate().size() >1){
            vo.setStartDate(vo.getWorkDate().get(0));
            vo.setEndDate(vo.getWorkDate().get(1));
        }
        Page<AttendanceWorkOver> page = new Page<>(vo.getPageNo(), vo.getPageSize());
        page = attendanceWorkOverService.selectAttendanceWorkOverPage(page,vo);
        return new ResultJsonUtil().success(page);
    }

    @ApiOperation(value = "获取考勤异常列表")
    @PostMapping("/selectWorkeOverAbnormal")
    public ResultJsonUtil<Object> selectWorkeOverAbnormal(@RequestBody(required = false)AttendanceWorkOverVO vo,
                                                      Authentication authentication,
                                                      HttpServletRequest request) {
        Claims claims = JwtUtil.getJwtInfo(authentication);
        String userName = (String) claims.get("user_name");
        vo.setUserName(userName);
        if(vo.getWorkDate() != null && vo.getWorkDate().size() >1){
            vo.setStartDate(vo.getWorkDate().get(0));
            vo.setEndDate(vo.getWorkDate().get(1));
        }
        Page<AttendanceWorkOver> page = new Page<>(vo.getPageNo(), vo.getPageSize());
        page = attendanceWorkOverService.selectWorkeOverAbnormal(page,vo);
        return new ResultJsonUtil().success(page);
    }


    @ApiOperation(value = "修改可用调休状态")
    @GetMapping("/updateStatus/{id}")
    public ResultJsonUtil<Object> updateStatus(@PathVariable("id")Integer id,
                                                      HttpServletRequest request) {
        return new ResultJsonUtil().success(attendanceWorkOverService.updateStatus(id));
    }

    @ApiOperation(value = "考勤统计数量")
    @PostMapping("/selectWorkeOverStatistics")
    public ResultJsonUtil<Object> selectWorkeOverStatistics(@RequestBody(required = false)List<String> dates,
                                                          Authentication authentication,
                                                          HttpServletRequest request) {
        Claims claims = JwtUtil.getJwtInfo(authentication);
        String userName = (String) claims.get("user_name");
        Map<String,Object> map = new HashMap<>();
        map.put("userName",userName);
        if(dates != null && dates.size() >1){
            map.put("startDate",dates.get(0));
            map.put("endDate",dates.get(1));
        }
        JSONObject jsonObject  = attendanceWorkOverService.selectWorkeOverStatistics(map);
        return new ResultJsonUtil().success(jsonObject);
    }


}